#!/usr/local/bin/python
import sshexec
from random import *


class Coordinator(object):
	version = 0.1
	def __init__(self,name):
		self.name=name
	def genPass(self,length):
		passgenstr='qwertyuiopasdfghjklzxcvbnm'
		passgenstr+='qwertyuiopasdfghjklzxcvbnm'.upper()
		passgenstr+='1234567890_'
		passwd=''
		for i in range(length) :
			passwd+=passgenstr[randint(0,len(passgenstr)-1)] 
		return passwd
	def coordinate(self, script, userlist, hostlist):
		if not script : return [['SCRIPT NOT FOUND']]
		if not userlist: return [['USERLIST NOT FOUND']]
		if not hostlist: return [['HOSTLIST NOT FOUND']]
		
		scriptlist=script.split('\n')
		paramlist=[]
		remote=[]
	
#		for user in userlist:
#			passwd = self.genPass(7)
#			paramlist = list([user.login,passwd])
		for host in hostlist:
			for user in userlist:
				passwd = self.genPass(7)
				paramlist = list([user.login,passwd])
				logs=sshexec.Logger()
				reo=sshexec.RemoteExecute(scriptlist,paramlist,'root',host)
				state=reo.remoteExec()
				if not state :st='OK'
				else :st='ERROR'
				logs.logGlobal(self.name, user.login, host, state)# [None, None])#state)
				remote.append([user,passwd,host,st])
		return remote	